package org.dromara.llm.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.llm.domain.vo.SysTemplateVo;
import org.dromara.llm.domain.bo.SysTemplateBo;
import org.dromara.llm.service.ISysTemplateService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 提示词模板
 *
 * @author Miles Meng
 * @date 2025-06-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/llm/template")
public class SysTemplateController extends BaseController {

    private final ISysTemplateService sysTemplateService;

    /**
     * 查询提示词模板列表
     */
    @SaCheckPermission("system:template:list")
    @GetMapping("/list")
    public TableDataInfo<SysTemplateVo> list(SysTemplateBo bo, PageQuery pageQuery) {
        return sysTemplateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出提示词模板列表
     */
    @SaCheckPermission("system:template:export")
    @Log(title = "提示词模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysTemplateBo bo, HttpServletResponse response) {
        List<SysTemplateVo> list = sysTemplateService.queryList(bo);
        ExcelUtil.exportExcel(list, "提示词模板", SysTemplateVo.class, response);
    }

    /**
     * 获取提示词模板详细信息
     *
     * @param templateId 主键
     */
    @SaCheckPermission("system:template:query")
    @GetMapping("/{templateId}")
    public R<SysTemplateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long templateId) {
        return R.ok(sysTemplateService.queryById(templateId));
    }

    /**
     * 新增提示词模板
     */
    @SaCheckPermission("system:template:add")
    @Log(title = "提示词模板", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysTemplateBo bo) {
        return toAjax(sysTemplateService.insertByBo(bo));
    }

    /**
     * 修改提示词模板
     */
    @SaCheckPermission("system:template:edit")
    @Log(title = "提示词模板", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysTemplateBo bo) {
        return toAjax(sysTemplateService.updateByBo(bo));
    }

    /**
     * 删除提示词模板
     *
     * @param templateids 主键串
     */
    @SaCheckPermission("system:template:remove")
    @Log(title = "提示词模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] templateids) {
        return toAjax(sysTemplateService.deleteWithValidByIds(List.of(templateids), true));
    }
}
